const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  resolve: {
    extensions: [".ts", ".tsx", ".js", ".jsx", ".json", ".scss"],
  },
  entry: "./src/main.ts",
  output: {
    filename: "app-[name]-[contenthash].js",
    path: path.resolve(__dirname, "./dist"),
    clean: true,
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        loader: "ts-loader",
      },
      {
        test: /\.js$/,
        loader: "source-map-loader",
      },
      {
        test: /\.(scss|sass)$/i, // 匹配 .scss 和 .sass 文件
        use: [
          "style-loader", // 第三步：将 JS 字符串生成为 style 节点（开发环境）
          "css-loader", // 第二步：将 CSS 转化成 CommonJS 模块
          "sass-loader", // 第一步：将 Sass 编译成 CSS
        ],
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i, // 处理图片资源
        type: "asset/resource",
        generator: {
          filename: "images/[hash][ext][query]",
        },
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i, // 处理字体资源
        type: "asset/resource",
        generator: {
          filename: "fonts/[hash][ext][query]",
        },
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./src/index.html",
    }),
  ],
};
